package 栈算法;

import java.util.Stack;

public class MinStack155 {
    /**
     * 最小栈
     */

    Stack<Integer> stack;

    public MinStack155() {
        stack = new Stack<Integer>();
    }

    public void push(int val) {
        if (stack.isEmpty()) {
            stack.add(val);
            stack.add(val);
        } else {
            int value = stack.peek();
            if (value >= val) {
                stack.add(val);
                stack.add(val);
            } else {
                stack.add(val);
                stack.add(value);
            }
        }
    }

    public void pop() {
        stack.pop();
        stack.pop();
    }

    public int top() {
        int n1 = stack.pop();
        int n2 = stack.pop();
        stack.add(n2);
        stack.add(n1);
        return n2;
    }

    public int getMin() {
        int tmp = stack.peek();
        return tmp;
    }
}
